﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataLayer.Infrastructure;
using ModelLayer;

namespace BusinessLayer
{
    public class CategoryManager
    {

        public IEnumerable<Category> GetAllCategories(bool withSubCategories = false)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.CategoryRepo.Get();
            }

        }

        public IEnumerable<Category> GetLeafCategories()
        {
            using (var uow = new UnitOfWork())
            {
                return uow.CategoryRepo.Get().Where(x => x.SubCategories == null);
            }
        }

        public Category GetById(long categoryId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.CategoryRepo.GetById(categoryId);
            }
        }

        public bool AddUpdate(Category category)
        {
            using (var uow = new UnitOfWork())
            {
                if (category.CategoryId > 0)
                {
                    uow.CategoryRepo.Update(category);
                }
                else
                {
                    uow.CategoryRepo.Insert(category);
                }

                return uow.SaveChanges() > 0;
            }
        }

        public bool Delete(long categoryId)
        {
            using (var uow = new UnitOfWork())
            {
                var category = uow.CategoryRepo.GetById(categoryId);
                uow.CategoryRepo.Delete(category);

                return uow.SaveChanges() > 0;
            }
        }
    }
}
